package com.seeTools.next.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysConfig;
import com.seeTools.next.common.constant.Constants;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.mapper.sys.SysConfigMapper;
import com.seeTools.next.service.BaseServiceImpl;
import com.seeTools.next.service.sys.SysConfigService;
import com.seeTools.next.util.PageQueryHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2024/5/7  10:11
 * @Version 1.0
 */
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {


    @Resource
    private PageQueryHelper<SysConfig> pageQueryHelper;

    /**
     * 分页查询
     *
     * @param pageQuery 分页数据
     * @return 对象
     */
    @Override
    public Page<SysConfig> findList(PageQuery<SysConfig> pageQuery) {
        QueryWrapper<SysConfig> queryWrapper = getQueryWrapper(pageQuery.getParams());
        return dictionary(pageQueryHelper.pageInfo(this, pageQuery, queryWrapper));
    }


    /**
     * 查询数据
     *
     * @param model
     * @return
     */
    @Override
    public List<SysConfig> selectDataList(SysConfig model) {
        QueryWrapper<SysConfig> queryWrapper = getQueryWrapper(model);
        return dictionary(list(queryWrapper));
    }


    /**
     * 校验参数键名是否唯一
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public boolean checkConfigKeyUnique(SysConfig config) {
        Long configId = ObjectUtil.isEmpty(config.getId()) ? -1L : config.getId();
        SysConfig info = findOne(SysConfig::getConfigKey, config.getConfigKey());
        if (ObjectUtil.isNotNull(info) && info.getId().longValue() != configId.longValue()) {
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }


    /**
     * 根据key修改数据
     *
     * @param sysConfig
     * @return
     */
    @Override
    public boolean updateByKey(SysConfig sysConfig) {
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfig::getConfigKey, sysConfig.getConfigKey());
        SysConfig result = getOne(lambdaQueryWrapper, false);
        result.setConfigValue(sysConfig.getConfigValue());
        return updateById(result);
    }

    /**
     * 根据key获取value
     *
     * @param key
     * @return
     */
    @Override
    public String getValueByKey(String key) {
        SysConfig sysConfig = findOne(SysConfig::getConfigKey, key);
        if (ObjectUtil.isNotEmpty(sysConfig)) {
            return sysConfig.getConfigValue();
        }
        return null;
    }


    /**
     * 获取查询条件
     *
     * @param model
     * @return
     */
    public QueryWrapper<SysConfig> getQueryWrapper(SysConfig model) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = queryWrapper.lambda();
        if (ObjectUtil.isNotEmpty(model)) {

        }
        return queryWrapper;
    }


    /**
     * 翻译数据
     *
     * @param page
     * @return
     */
    public Page<SysConfig> dictionary(Page<SysConfig> page) {
        if (ObjectUtil.isNotEmpty(page)) {
            List<SysConfig> list = page.getRecords();
            dictionary(list);
        }
        return page;
    }

    /**
     * 翻译
     *
     * @param list
     * @return
     */
    public List<SysConfig> dictionary(List<SysConfig> list) {
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(item -> {
            });
        }
        return list;
    }

}
